#!/user/bin/env python
# -*- coding:utf-8 -*-
#/home/zc/workspace/tinyfeet/trunk/src/controllers/updatetfeet.py

'''

发布 更新：
   {
            "uid":1223,
            "tfid":None,    --不等于None为update，否则为新增
            "text":"text body",
            "date":"时间戳",
            "long":132.22,
            "lat":22.22,
            "location":"中国,湖南,凤凰城",
            }
            
Created on 2011-10-25

@author: zc

changeLog:
2011-11-22 返回增加tfid
'''

import web
import simplejson
from checkkey import checkkey
from  updatetfeet_handler import newTFeet, updateTFeet
from controllers.utils import logger
from controllers.utils.upload import upload
logger = logger.logger()

class updateTFeet( object ):
    '''    新增 更新 tfeet    '''
    def GET( self ):
        return 'i got it. please go back.'

    def POST( self ):
        """ 接受客户端post的消息 """
        web.header( 'Content-Type', 'Content-Type: multipart/form-data;boundary=--------AaB03x' ) 
        data = web.input( myfile = {} )
        print data
#        print 'data.tfid:', data.tfid
#        print 'data.uid:', data.uid
#        print 'data.text:', data.text
#        print 'data.date:', data.date
#        print 'data.long:', data.long 
#        print 'data.lat:', data.lat 
#        print 'data.location:', data.location
        res = None
        if checkkey( web.config._session, web.ctx.env ):
            try:
                smallPic, norPic, orgPic = upload( data ) #上传文件
                print smallPic, norPic, orgPic
                if smallPic is not None and  norPic is not None and orgPic is not None:
                    
                    tfid = data.tfid if data.tfid <> 'null' else None
                    tfeet = {
                                u'tfid' : tfid,
                                u'uid' : data.uid,
                                u'text' : data.text.replace( '\n', '' ).replace( '\r', '' ),
                                u'date' : data.date,
                                u'pic_normal' : norPic,
                                u'pic_small' : smallPic,
                                u'pic_original' : orgPic,
                                u'long' : data.long,
                                u'lat' : data.lat,
                                u'location' : data.location,
                                u'weibo_id': None,
                                u'source':None
                                }
                    if tfid is None:
                        print 'newTFeet'
                        res = newTFeet( tfeet )
                        print res
                    else:
                        res = updateTFeet( tfeet )
                else:
                    res = '{"msg":"209", "errmsg":"上传图片失败."}' 
            except Exception, data:
                res = '{"msg":"204", "errmsg":"取值失败.%s"}' % data 
                #print data
                #logger.error( data )
        else:
                res = '{"msg":"203", "errmsg":"session验证未通过."}'
        return res


